/**
 * @author yexiebao
 * @date 2025/5/13
 */
public class MinSubArrayLen {
    /**
     *错误解法
     */
    public static int minSubArrayLenShit(int[] nums,int target){
        for(int i = 0;i<nums.length;i++){
            System.out.print(nums[i]+" ");
        }
        System.out.println("\n");
        int sum = 0;
        int k = 0;
        for(int i = 0;i<nums.length;i++){
            for(int j = 0;j<nums.length-i-1;j++){
                if(nums[j]<nums[j+1]){
                    int temp = nums[j];
                    nums[j] = nums[j+1];
                    nums[j+1] = temp;
                }
            }
        }
        for(int i = 0;i<nums.length;i++){
            System.out.print(nums[i]+" ");
        }
        System.out.println("\n");
        while(sum < target){
            sum += nums[k];
            k++;
            System.out.println(sum + "\n");
            System.out.println(k + "\n");
        }
        return k;
    }
    /**
     *暴力解法
     */
    public static int minSubArrayLen(int[] nums,int target){
        //1.定义一个最小长度
        int minLen = Integer.MAX_VALUE;
        for(int i = 0; i<nums.length; i++){
            int sum = 0;
            for (int j = i; j <nums.length; j++){
                sum += nums[j];
                if(sum>=target){
                    if(minLen > j-i+1){
                        minLen = j-i+1;
                        break;
                    }
                }
            }
        }
        return minLen==Integer.MAX_VALUE?0:minLen;
    }
    /**
     *滑动窗口解法
     */
    public static int minSubArrayLenUseSlideWindow(int[] nums,int target) {
        //1.定义一个最小长度
        int minLen = Integer.MAX_VALUE;
        //2.定义一个滑动窗口的左边界
        int left = 0;
        //3.定义一个元素和
        int sum = 0;
        for(int right = 0;right<nums.length;right++){
            sum += nums[right];//拓展右边界
            while(sum>=target){//发现超过target后再收缩左边界
                minLen = Math.min(minLen,right -left +1);//这才是最终需要的！！！
                sum -= nums[left];
                left++;
            }
        }
        return minLen==Integer.MAX_VALUE?0:minLen;
    }


    public static void main(String[] args) {
        int[] nums = {12,28,83,4,25,26,25,2,25,25,25,12};
        System.out.println(minSubArrayLen(nums,213));
    }
}